package com.icw.configuration.dict;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import com.icw.configuration.data_interchange.RpcResult;
import com.icw.decoupling.defi.tool.DictManager;
import com.icw.utility.BeanCtxUtils;
import com.icw.utility.IcwUtils;

@Controller
public class DictionaryConfig {

	@GetMapping("/statics/dictionary")
	public void dictionary(HttpServletResponse resp) throws Exception {
		Map<String, Object> data = new LinkedHashMap<>();
		DictManager dm = BeanCtxUtils.getBeanByType(DictManager.class);
		Set<String> dictGroupNames = dm.dictGroupNames();
		List<Object> list = new LinkedList<>();
		for (String dictGroupName : dictGroupNames) {
			Map<String, String> map = dm.dictGroup(dictGroupName);
			for (Entry<String, String> entry : map.entrySet()) {
				list.add(IcwUtils.mapObject("values", new Object[] { entry.getKey(), entry.getValue(), dictGroupName }));
			}
		}
		data.put("list", list);
		IcwUtils.render(resp, new RpcResult(true, null, data));
	}
}
